using System.Collections.Generic;
using Token = TBit.Compile.ASMCompile.Token;
using TBit.Compile;

namespace TBit.Instrcutions {
    public static class Ret {
    
        public static int Compile(List<Token> tokens,VM vm,int i) {
            return ASMCompile.StructureInstruction_OPCODE_DVR(Opcode.RET,tokens,vm,i);
        }
        public static Instruction Create(Data a) {
            Instruction inst = new Instruction();
            inst.opcode = Opcode.RET;
            inst.args.Add(new InstructionArgData(a));
            return inst;
        }

        public static Instruction Create(string a) {
            Instruction inst = new Instruction();
            inst.opcode = Opcode.RET;
            inst.args.Add(new InstructionArgData(a));
            return inst;
        }

        public static Instruction Create(RegisterIndex a) {
            Instruction inst = new Instruction();
            inst.opcode = Opcode.RET;
            inst.args.Add(new InstructionArgData(a));
            return inst;
        }

        public static int Run(VM vm,Instruction inst){
            InstructionArgData arg1 = inst.args[0];
            Data ret_value = Mov.GetDVRData(vm,ref arg1);
            vm.WriteRegister(RegisterIndex.RET,ret_value);
            Data ret_address = vm.MemoryPop();
            if(ret_address.type != DataType.Number) throw new VMRunError("函数调用错误");
            vm.pc = (int)ret_address.number_data;
            return 0;
        }
    }
}